package com.dqhl.business.Base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.widget.Toast;

import com.dqhl.business.MyApplication;
import com.dqhl.business.modle.User;
import com.dqhl.business.util.UserSaveUtil;
import com.dqhl.business.view.CustomProgressDialog;

/**
 * Created by mac on 16/11/2.
 * BaseFragment
 */
public class BaseFragment extends Fragment {

    private Toast mToast;
    protected MyApplication app;
    private CustomProgressDialog mCustomProgressDialog;
    protected User user;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        app = (MyApplication) getActivity().getApplicationContext();
        user = (User) UserSaveUtil.readUser(getActivity());
    }

    /**
     * Toast
     *
     * @param resId 资源信息
     */
    public void toast(int resId) {
        String msg = app.getResources().getString(resId);
        mToast = Toast.makeText(app, msg, Toast.LENGTH_SHORT);
        mToast.show();
    }

    /**
     * Toast
     *
     * @param msg 文字信息
     */
    public void toast(String msg) {
        mToast = Toast.makeText(app, msg, Toast.LENGTH_SHORT);
        mToast.show();
    }

    /**
     * 直接跳转到另一个Activity界面
     *
     * @param classObj ***Activity.class
     */
    public void overlay(Class<?> classObj) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setClass(getActivity(), classObj);
        startActivity(intent);
    }

    /**
     * 直接跳转到另一个Activity，并带有bundle参数
     *
     * @param classObj class对象
     * @param params   bundle参数
     */
    public void overlay(Class<?> classObj, Bundle params) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setClass(getActivity(), classObj);
        intent.putExtras(params);
        startActivity(intent);
    }

    /**
     * 以satartActivityForResult方式启动另一个Activity
     *
     * @param classObj    class 对象
     * @param requestCode 请求码
     */
    public void overlay(Class<?> classObj, int requestCode) {
        Intent intent = new Intent(getActivity(), classObj);
        startActivityForResult(intent, requestCode);
    }

    /**
     * 启动另一个Activity，并结束当前Activity
     *
     * @param classObj 要跳转的activity
     */
    public void forward(Class<?> classObj) {
        Intent intent = new Intent(getActivity(), classObj);
        startActivity(intent);
        getActivity().finish();
    }

    /**
     * 带有Bundle参数的启动另一个Activity，并结束当前Activity
     *
     * @param classObj 要跳转的activity
     * @param bundle   要传递的Bundle
     */
    public void forward(Class<?> classObj, Bundle bundle) {
        Intent intent = new Intent(getActivity(), classObj);
        intent.putExtras(bundle);
        startActivity(intent);
        getActivity().finish();
    }

    /**
     * 显示自定义ProgressBar，提示用户操作正在处理中
     *
     * @param content:要显示的提示内容包括ProgressBar和text
     */
    public void showCustomLoadBar(String content) {
        if (mCustomProgressDialog == null) {
            mCustomProgressDialog = new CustomProgressDialog(getActivity(), content);
            mCustomProgressDialog.setCanceledOnTouchOutside(false);
        } else {
            mCustomProgressDialog.setTitle(content);
        }
        mCustomProgressDialog.show();
    }

    /**
     * 释放自定义ProgressBar，提示操作结果
     */
    public void hideCustomLoadBar() {
        if (mCustomProgressDialog != null && mCustomProgressDialog.isShowing()) {
            mCustomProgressDialog.cancel();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mToast != null) {
            mToast.cancel();
        }
    }
}
